package com.example.diagnodroid.mouvement;

import android.app.Activity;
import android.content.Context;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.os.Bundle;
import android.view.Window;
import android.widget.TextView;

import com.example.diagnodroid.R;

public class AccelerometerActivity extends Activity implements SensorEventListener {
	
	protected static final int MOVE_IDENTIFER = 0x101;
	SensorManager sensorManager;
	Thread refreshThread = null;
	
	BounceView bounceView = null;
	TextView textView = null;
	private TextView name;
	private TextView type;
	private TextView precision;
	private TextView raffraichissement;
	private TextView x;
	private TextView y;
	private TextView z;
	

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		requestWindowFeature(Window.FEATURE_NO_TITLE);

		setContentView(R.layout.accelerometer_layout);

		bounceView = (BounceView) findViewById(R.id.idBounceView);
		
		name = (TextView) findViewById(R.id.resName);
		type = (TextView) findViewById(R.id.resType);
		precision = (TextView) findViewById(R.id.resPrecision);
		raffraichissement = (TextView) findViewById(R.id.resRaffraichissement);
		x = (TextView) findViewById(R.id.resX);
		y = (TextView) findViewById(R.id.resY);
		z = (TextView) findViewById(R.id.resZ);
		
		sensorManager = (SensorManager) getSystemService(Context.SENSOR_SERVICE);

	}

	protected void onResume() {
		super.onResume();
		sensorManager.registerListener(this,
				sensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER),
				SensorManager.SENSOR_DELAY_GAME);
	}

	protected void onStop() {
		sensorManager.unregisterListener(this);
		super.onStop();
	}
	
	@Override
	public void onAccuracyChanged(Sensor sensor, int accuracy) {
		
	}

	@Override
	public void onSensorChanged(SensorEvent event) {
		if (event.sensor.getType() == Sensor.TYPE_ACCELEROMETER) {
			bounceView.mPitch = event.values[0];
			bounceView.mHeading = event.values[1];
			
			bounceView.invalidate();
			
			name.setText(event.sensor.getName());
			type.setText("Accelerometre");
			precision.setText(""+event.sensor.getResolution());
			raffraichissement.setText(""+event.sensor.getMinDelay());
			x.setText(""+event.values[0]);
			y.setText(""+event.values[1]);
			z.setText(""+event.values[2]);
		}
		
		

	}
}
